package net.scrumtool.controller;

import java.awt.event.MouseAdapter;

import net.scrumtool.model.Collaborator;
import net.scrumtool.model.Product;
import net.scrumtool.model.Sprint;
import net.scrumtool.technics.DAO;
import net.scrumtool.technics.LabelProviderSprint;
import net.scrumtool.view.Home;
import net.scrumtool.view.OverviewProduct;
import net.scrumtool.view.OverviewSprint;

import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.MessageBox;

public class Cdashboard {

	Home view;
	private Product productActif;
	private Sprint sprintActif;

	public Cdashboard(Home view) {
		this.view = view;
	}

	public void init(Collaborator userActif) {

		view.getTableViewerProductDashboard().refresh();
		view.getTableViewerProductDashboard().setContentProvider(new ArrayContentProvider());
		view.getTableViewerProductDashboard().setLabelProvider(new LabelProvider());
		view.getTableViewerProductDashboard().setInput(DAO.getProductByCollaborator(userActif));
		
		view.getTltmOpenProductDashboard().setEnabled(false);
		view.getTltmOpenSprintDashboard().setEnabled(false);
	}

	public void listener(final Collaborator userActif) {

		view.getTableProductDashboard().addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				StructuredSelection sel = (StructuredSelection) view.getTableViewerProductDashboard().getSelection();
				setProductActif((Product) sel.getFirstElement());
				
				view.getTableViewerSprintDashboard().setContentProvider(new ArrayContentProvider());
				view.getTableViewerSprintDashboard().setLabelProvider(new LabelProviderSprint());
				view.getTableViewerSprintDashboard().setInput(DAO.getSprintsByNameProduct(productActif.getName()));
				

				view.getTltmOpenProductDashboard().setEnabled(true);
				view.getTltmOpenSprintDashboard().setEnabled(false);
			}
		});

		view.getTableSprintDashboard().addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				StructuredSelection sel = (StructuredSelection) view.getTableViewerSprintDashboard().getSelection();
				setSprintActif((Sprint) sel.getFirstElement());

				view.getTltmOpenProductDashboard().setEnabled(false);
				view.getTltmOpenSprintDashboard().setEnabled(true);
			}
			
		});

		view.getTltmOpenProductDashboard().addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {
					OverviewProduct overview = new OverviewProduct();
					overview.init();
					CoverviewProduct cOverview = new CoverviewProduct(overview);
					cOverview.listener(userActif,productActif);
					cOverview.init(userActif, productActif);
					cOverview.reload(userActif, productActif);
					view.close();
					overview.open();
			}
		});
		
		
		view.getTableProductDashboard().addMouseListener(new MouseListener() {

			@Override
			public void mouseDoubleClick(
					org.eclipse.swt.events.MouseEvent arg0) {
				
				StructuredSelection sel = (StructuredSelection) view.getTableViewerProductDashboard().getSelection();
				setProductActif((Product) sel.getFirstElement());
				OverviewProduct overview = new OverviewProduct();
				overview.init();
				CoverviewProduct cOverview = new CoverviewProduct(overview);
				cOverview.listener(userActif,productActif);
				cOverview.init(userActif, productActif);
				cOverview.reload(userActif, productActif);
				view.close();
				overview.open();
				

			}

			@Override
			public void mouseDown(org.eclipse.swt.events.MouseEvent arg0) {

			}

			@Override
			public void mouseUp(org.eclipse.swt.events.MouseEvent arg0) {
				// TODO Auto-generated method stub

			}
		});
		
		
		
		
		
		view.getTableSprintDashboard().addMouseListener(new MouseListener() {

			@Override
			public void mouseDoubleClick(
					org.eclipse.swt.events.MouseEvent arg0) {
				
				StructuredSelection sel = (StructuredSelection) view.getTableViewerSprintDashboard().getSelection();
				setSprintActif((Sprint) sel.getFirstElement());
				OverviewSprint overview = new OverviewSprint();
				overview.init();
				CoverviewSprint cOvS = new CoverviewSprint(overview);
				cOvS.listener(userActif);
				cOvS.init(sprintActif);
				view.close();
				overview.open();
			}

			@Override
			public void mouseDown(org.eclipse.swt.events.MouseEvent arg0) {

			}

			@Override
			public void mouseUp(org.eclipse.swt.events.MouseEvent arg0) {
				// TODO Auto-generated method stub

			}
		});
		
		

		view.getTltmOpenSprintDashboard().addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {
					OverviewSprint overview = new OverviewSprint();
					overview.init();
					CoverviewSprint cOvS = new CoverviewSprint(overview);
					cOvS.listener(userActif);
					cOvS.init(sprintActif);
					view.close();
					overview.open();
			}
		});

	}

	public void setProductActif(Product productActif) {
		this.productActif = productActif;
	}

	public void setSprintActif(Sprint sprintActif) {
		this.sprintActif = sprintActif;
	}

}
